package common;

import java.util.Random;




/*
 * This class is to work how to allocate the ship on 
 * the map and some related algorithms.
 */
public class Tools {
	private String[][] ShipLoction=new String[6][6];
	private Ship shipb;
	private Ship shipc;
	private Ship shipd;
	private Ship ships;
	private Random random=new Random();;

	public Tools(){
		shipb=new Ship("b",4);
		shipc=new Ship("c",3);
		shipd=new Ship("d",2);
		ships=new Ship("s",1);

	}

	public synchronized void manageShip(){
		
		for(int i=0;i<6;i++){
			for(int j=0;j<6;j++){
				ShipLoction[i][j]=" ";
			}
		}
		
		int randomRow;
		int randomCol;
		int lineKind;
		randomRow=random.nextInt(6);            //print b
		randomCol=random.nextInt(6);
		lineKind=random.nextInt(3);

		switch (lineKind){
		case 0:   
			if(randomRow>=shipb.getSize()-1){
				for(int i=0;i<shipb.getSize();i++){
					ShipLoction[randomRow-i][randomCol]=shipb.getName();
				} 
			}else{
				for(int i=0;i<shipb.getSize();i++){
					ShipLoction[randomRow+i][randomCol]=shipb.getName();
				} 
			}
			break;
		case 1:   if(randomCol>=shipb.getSize()-1){
			for(int i=0;i<shipb.getSize();i++){
				ShipLoction[randomRow][randomCol-i]=shipb.getName();
			} 
		}else{
			for(int i=0;i<shipb.getSize();i++){
				ShipLoction[randomRow][randomCol+i]=shipb.getName();
			} 
		}
		break;
		case 2:   
			if(randomRow>=3&&randomCol<3){
				for(int i=0;i<shipb.getSize();i++){
					ShipLoction[randomRow-i][randomCol+i]=shipb.getName();
				} 
			}else if(randomRow>=3&&randomCol>=3){
				for(int i=0;i<shipb.getSize();i++){
					ShipLoction[randomRow-i][randomCol-i]=shipb.getName();
				} 
			}else if(randomRow<3&&randomCol>=3){
				for(int i=0;i<shipb.getSize();i++){
					ShipLoction[randomRow+i][randomCol-i]=shipb.getName();
				} 
			}else if(randomRow<3&&randomCol<3){
				for(int i=0;i<shipb.getSize();i++){
					ShipLoction[randomRow+i][randomCol+i]=shipb.getName();
				} 
			}
			break;
		}

		int randomRow1=0;              //print c
		int randomCol1=0;
		int lindkind1;
		randomRow1=random.nextInt(4)+1;
		randomCol1=random.nextInt(4)+1;
		boolean exist=false;

		lindkind1=random.nextInt(4);

		if(randomRow1+1>5||randomCol1+1>5){
			System.out.println("over");
		}else{

			switch (lindkind1){
			case 0: 
				System.out.println("0");

				while(!exist){
					exist=ShipLoction[randomRow1+1][randomCol1].equals(" ")
					&&ShipLoction[randomRow1-1][randomCol1].equals(" ")
					&&ShipLoction[randomRow1][randomCol1].equals(" ");
					if(exist){
						ShipLoction[randomRow1][randomCol1]=shipc.getName();
						ShipLoction[randomRow1-1][randomCol1]=shipc.getName();
						ShipLoction[randomRow1+1][randomCol1]=shipc.getName();

					}

					randomRow1=random.nextInt(4)+1;
					randomCol1=random.nextInt(4)+1;
				}
				break;
			case 1:
				System.out.println("1");
				System.out.println(randomRow1);
				System.out.println(randomCol1);
				while(!exist){
					exist=ShipLoction[randomRow1][randomCol1+1].equals(" ")
					&&ShipLoction[randomRow1][randomCol1-1].equals(" ")
					&&ShipLoction[randomRow1][randomCol1].equals(" ");
					if(exist){
						ShipLoction[randomRow1][randomCol1]=shipc.getName();
						ShipLoction[randomRow1][randomCol1-1]=shipc.getName();
						ShipLoction[randomRow1][randomCol1+1]=shipc.getName();
					}
					randomRow1=random.nextInt(4)+1;
					randomCol1=random.nextInt(4)+1;
					System.out.print("loop or not   "+exist);
				}
				break;
			case 2:	
				System.out.println("2");
				System.out.println(randomRow1);
				System.out.println(randomCol1);

				while(!exist){
					exist=ShipLoction[randomRow1+1][randomCol1+1].equals(" ")
					&&ShipLoction[randomRow1-1][randomCol1-1].equals(" ")
					&&ShipLoction[randomRow1][randomCol1].equals(" ");
					if(exist){
						System.out.println("step1");
						ShipLoction[randomRow1][randomCol1]=shipc.getName();
						ShipLoction[randomRow1-1][randomCol1-1]=shipc.getName();
						ShipLoction[randomRow1+1][randomCol1+1]=shipc.getName();
					}

					randomRow1=random.nextInt(4)+1;
					randomCol1=random.nextInt(4)+1;
				}
				break;
			case 3:
				System.out.println("3");

				while(!exist){
					exist=ShipLoction[randomRow1-1][randomCol1+1].equals(" ")
					&&ShipLoction[randomRow1+1][randomCol1-1].equals(" ")
					&&ShipLoction[randomRow1][randomCol1].equals(" ");
					if(exist){
						ShipLoction[randomRow1][randomCol1]=shipc.getName();
						ShipLoction[randomRow1+1][randomCol1-1]=shipc.getName();
						ShipLoction[randomRow1-1][randomCol1+1]=shipc.getName();
					}

					randomRow1=random.nextInt(4)+1;
					randomCol1=random.nextInt(4)+1;
				}
				break;
			}
		}

		int randomRow2=0;              //print d
		int randomCol2=0;
		boolean existt=false;
		randomRow2=random.nextInt(4)+1;
		randomCol2=random.nextInt(4)+1;
		int lindkind2;
		lindkind2=random.nextInt(4);
		switch (lindkind2){
		case 0: 
			while(!existt){
				existt=ShipLoction[randomRow2+1][randomCol2].equals(" ")
				&&ShipLoction[randomRow2][randomCol2].equals(" ");
				if(existt){
					ShipLoction[randomRow2][randomCol2]=shipd.getName();
					ShipLoction[randomRow2+1][randomCol2]=shipd.getName();

				}

				randomRow2=random.nextInt(4)+1;
				randomCol2=random.nextInt(4)+1;
			}
			break;
		case 1:
			while(!existt){
				existt=ShipLoction[randomRow2][randomCol2-1].equals(" ")
				&&ShipLoction[randomRow2][randomCol2].equals(" ");
				if(existt){
					ShipLoction[randomRow2][randomCol2]=shipd.getName();
					ShipLoction[randomRow2][randomCol2-1]=shipd.getName();
				}	
				randomRow2=random.nextInt(4)+1;
				randomCol2=random.nextInt(4)+1;

			}
			break;
		case 2:	


			while(!existt){
				existt=ShipLoction[randomRow2+1][randomCol2+1].equals(" ")
				&&ShipLoction[randomRow2][randomCol2].equals(" ");
				if(existt){
					System.out.println("step1");
					ShipLoction[randomRow2][randomCol2]=shipd.getName();
					ShipLoction[randomRow2+1][randomCol2+1]=shipd.getName();
				}

				randomRow2=random.nextInt(4)+1;
				randomCol2=random.nextInt(4)+1;
			}
			break;
		case 3:

			while(!existt){
				existt=ShipLoction[randomRow2-1][randomCol2+1].equals(" ")
				&&ShipLoction[randomRow2][randomCol2].equals(" ");
				if(existt){
					ShipLoction[randomRow2][randomCol2]=shipd.getName();
					ShipLoction[randomRow2-1][randomCol2+1]=shipd.getName();
				}

				randomRow2=random.nextInt(4)+1;
				randomCol2=random.nextInt(4)+1;
			}
			break;
		}


		int randomRow3=0;              //print d
		int randomCol3=0;
		boolean existtt=false;
		randomRow3=random.nextInt(4)+1;
		randomCol3=random.nextInt(4)+1;
		int lindkind3;
		lindkind2=random.nextInt(4);
		while(!existtt){
			existtt=ShipLoction[randomRow3][randomCol3].equals(" ");
			if(existtt){
				ShipLoction[randomRow3][randomCol3]=ships.getName();
			}
			randomRow2=random.nextInt(4)+1;
			randomCol2=random.nextInt(4)+1;
		}
	}

	public synchronized String[][] getArray(){
		return ShipLoction;
	}

	/*public static void main(String[] args){
		Tools tool=new Tools();
		tool.manageShip();
		tool.print();

	}*/
}
